package cn.sdormitory.academic.service.impl;

import cn.sdormitory.academic.dao.ClsPracticeInfoDao;
import cn.sdormitory.academic.dao.ClsPracticeResultDao;
import cn.sdormitory.academic.entity.ClsPracticeInfo;
import cn.sdormitory.academic.entity.ClsPracticeResult;
import cn.sdormitory.academic.service.ClsPracticeInfoService;
import cn.sdormitory.academic.vo.ClsPracticeInfoQueryVo;
import cn.sdormitory.basedata.dao.BStudentDao;
import cn.sdormitory.basedata.entity.BStudent;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.alibaba.excel.write.metadata.fill.FillConfig;
import com.alibaba.excel.write.metadata.fill.FillWrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ClsPracticeInfoServiceImpl
        extends ServiceImpl<ClsPracticeInfoDao, ClsPracticeInfo>
        implements ClsPracticeInfoService {

    @Autowired
    ClsPracticeInfoDao clsPracticeInfoDao;

    @Autowired
    ClsPracticeResultDao clsPracticeResultDao;

    @Autowired
    BStudentDao bStudentDao;

    @Override
    public IPage<ClsPracticeInfo> findPageList(ClsPracticeInfoQueryVo vo) {
        int count = clsPracticeInfoDao.findPageListCount(vo);
        int startNum = (vo.getPageNum() - 1) * vo.getPageSize();
        List<ClsPracticeInfo> list = clsPracticeInfoDao.findPageList(startNum,vo.getPageSize(),vo);
        IPage<ClsPracticeInfo> page = new Page<>();
        page.setTotal(count);
        page.setRecords(list);
        return page;
    }

    @Override
    @Transactional
    public Boolean createOrUpdate(ClsPracticeInfo clsPracticeInfo) {
        if(clsPracticeInfo.getId() == null){
            clsPracticeInfoDao.insert(clsPracticeInfo);
            LambdaQueryWrapper<BStudent> stuWrapper = new LambdaQueryWrapper<BStudent>()
                    .eq(BStudent::getClassId, clsPracticeInfo.getClassId());
            List<BStudent> bStudents = bStudentDao.selectList(stuWrapper);
            for (BStudent bStudent :bStudents){
                ClsPracticeResult clsPracticeResult = new ClsPracticeResult();
                clsPracticeResult.setClsPracticeId(clsPracticeInfo.getId());
                clsPracticeResult.setStudentNo(bStudent.getStudentNo());
                clsPracticeResult.setStudentName(bStudent.getStudentName());
                clsPracticeResult.setClassId(bStudent.getClassId());
                clsPracticeResult.setClassName(bStudent.getClassName());
                clsPracticeResultDao.insert(clsPracticeResult);
            }
        }else {
            clsPracticeInfoDao.updateById(clsPracticeInfo);
        }
        return true;
    }

    @Override
    public ClsPracticeInfo findById(Long id) {
        return clsPracticeInfoDao.selectById(id);
    }

    @Override
    public List<ClsPracticeInfo> findClsPracticeInfo(ClsPracticeInfoQueryVo vo) {
        return clsPracticeInfoDao.findClsPracticeInfo(vo);
    }
}
